package top.callname.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

import top.callname.bean.AdminBean;
import top.callname.bean.ClassBean;
import top.callname.bean.NoticeBean;
import top.callname.bean.RspInfoBean;
import top.callname.service.ClassService;
import top.callname.service.NoticeService;
import top.callname.service.impl.ClassServiceImpl;
import top.callname.service.impl.NoticeServiceImpl;
import top.callname.util.BaseUtil;

@WebServlet("/admin/notice")
public class NoticeServlet extends HttpServlet {

	
	private Logger logger = Logger.getLogger(NoticeServlet.class);
	private ClassService classService = new ClassServiceImpl();
	private NoticeService noticeService = new NoticeServiceImpl();
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		AdminBean admin = (AdminBean) request.getSession().getAttribute("adminInfo");
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		if("list".equals(method)){ 
			//查询公告列表
			String pageNo = request.getParameter("pageNo");
			int page = BaseUtil.getPageNo(pageNo);
			request.setAttribute("page", noticeService.getListByPid(page, admin.getPid()));
			//查询班级列表
			List<ClassBean> list = classService.getList(admin.getPid());
			request.setAttribute("classList", list);
			request.getRequestDispatcher("notice.jsp").forward(request,response);
		
		}else if("save".equals(method)){
			String content = request.getParameter("content");
			String status = request.getParameter("status");
			String cid[] = request.getParameterValues("cids");
			String cids = "";
			String sendTime = request.getParameter("sendTime");
			for (int i = 0; i < cid.length; i++) {
				if(i != cid.length-1){
					cids+=cid[i]+",";
				}else{
					cids+=cid[i];
				}
			}
			NoticeBean noticeBean = new NoticeBean();
			noticeBean.setPid(admin.getPid());
			noticeBean.setCids(cids);
			noticeBean.setContent(content);
			noticeBean.setStatus(Integer.valueOf(status));
			RspInfoBean rsp = null;
			if("0".equals(status)){
				//发布定时公告
				noticeBean.setSendTime(sendTime);
				rsp = noticeService.addNoticeTask(noticeBean);
			}else if("1".equals(status)){
				//立即发送
				rsp = noticeService.addNoticeAndSend(noticeBean);
			}
			logger.debug("添加发布公告内容："+rsp.toString());
			out.write(JSON.toJSONString(rsp));
			
		}else if("close".equals(method)){
			
			String id = request.getParameter("id");
			RspInfoBean rsp = noticeService.closeNotice(Integer.valueOf(id));
			out.write(JSON.toJSONString(rsp));
			
		}else if("delete".equals(method)){
			
			String id = request.getParameter("id");
			RspInfoBean rsp = noticeService.deleteNotice(Integer.valueOf(id));
			out.write(JSON.toJSONString(rsp));
			
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
